// 终极Demo版PythonWrapper.hpp  
#include <dlfcn.h>  
#include <stdexcept>  

struct PyObject;  

class PythonVM {  
    void* _handle = nullptr;  
    bool _is_initialized = false;  

    // 核心API函数指针  
    using Py_Initialize_t = void(*)();  
    Py_Initialize_t Py_Initialize = nullptr;  
    // ...其他函数按需添加  

    // 函数指针成员
    using Py_Finalize_t = void(*)();
    using PyRun_SimpleString_t = int(*)(const char*);
    using PyUnicode_FromString_t = PyObject*(*)(const char*);
    using PyImport_ImportModule_t = PyObject*(*)(const char*);
    using PyObject_CallObject_t = PyObject*(*)(PyObject*, PyObject*);
    using Py_DecRef_t = void(*)(PyObject*);

    Py_Finalize_t Py_Finalize = nullptr;
    PyRun_SimpleString_t PyRun_SimpleString = nullptr;
    PyUnicode_FromString_t PyUnicode_FromString = nullptr;
    PyImport_ImportModule_t PyImport_ImportModule = nullptr;
    PyObject_CallObject_t PyObject_CallObject = nullptr;
    Py_DecRef_t Py_DecRef = nullptr;
public:  
    // 粗暴加载方式  
    void load_lib(const char* path) {  
        _handle = dlopen(path, RTLD_LAZY);  
        if (!_handle) throw std::runtime_error(dlerror());  

        #define LOAD_FUNC(name) \
            name = reinterpret_cast<name##_t>(dlsym(_handle, #name)); \
            if (!name) throw std::runtime_error("Fuck! 找不到" #name);  

        LOAD_FUNC(Py_Initialize);  
        LOAD_FUNC(Py_Finalize);
        LOAD_FUNC(PyRun_SimpleString);
        LOAD_FUNC(PyUnicode_FromString);
        LOAD_FUNC(PyImport_ImportModule);
        LOAD_FUNC(PyObject_CallObject);
        LOAD_FUNC(Py_DecRef);

        // 按需扩展其他函数  
    }  

    // 硬核调用示例  
    void exec(const char* code) {  
        if (!_is_initialized) {  
            Py_Initialize();  
            _is_initialized = true;  
        }  
        // 这里直接调PyRun_SimpleString，异常？不管！  
        PyRun_SimpleString(code);
    }  

    ~PythonVM() {  
        if (_handle) dlclose(_handle);  
    }  
};  

// 示例使用
int main(int argc, char* argv[]) {
    // PythonVM pythonVM(argc > 1 ? argv[1]);
    PythonVM vm;  
    vm.load_lib("/usr/lib/x86_64-linux-gnu/libpython3.11.so");  
    vm.exec("print('去他妈的优雅，能跑就行！')");
    vm.exec("import sys");
    vm.exec("print(sys.version)");
    return 0;
}